namespace Xsxiaosa.Net
{
    using System;
    using System.IO;
    using System.Net;
//jyj add
	using System.Net.Sockets;

    internal abstract class FtpData : IDisposable
    {
        // Methods
        public FtpData(Ftp ftp, Stream stream, FtpTransferState state)
        {
            this._transfer = false;
            this._disposed = false;
            this._length = ((long) 0);
            this._state = FtpTransferState.None;
            this._stream = null;
            this._ftpStream = null;
            this._array = new byte[65536];
            this._ftp = ftp;
            this._stream = stream;
            this._ftpStream = (stream as FtpStream);
            this._state = state;
            this._socket = ftp.ActiveProxy.CreateSocket();
        }

        public void Accept()
        {
            IAsyncResult result1 = this._socket.BeginAccept(null, null);
            try
            {
                this._ftp.WaitForCompletion(result1);
                this._socket.EndAccept(result1);
                this._transfer = true;
            }
            catch (Exception exception1)
            {
                this._socket.Close();
                throw exception1;
            }
        }

        public void Connect(IPEndPoint remote)
        {
            IAsyncResult result1 = this._socket.BeginConnect(remote, null, null);
            try
            {
                this._ftp.WaitForCompletion(result1);
                this._socket.EndConnect(result1);
                this._transfer = true;
            }
            catch (ProxySocketException exception1)
            {
                this._socket.Close();
                throw new FtpException(exception1.Message, exception1, FtpExceptionStatus.ConnectFailure);
            }
            catch (Exception exception2)
            {
                this._socket.Close();
                throw exception2;
            }
        }

        public void Dispose()
        {
            if (this._disposed)
            {
                return;
            }
            if (this._socket != null)
            {
                this._socket.Close();
                this._socket = null;
            }
            this._disposed = true;
            GC.SuppressFinalize(this);
        }

        protected abstract int DoTransfer();

        public IPEndPoint Listen(ProxySocket controlSocket)
        {
//jyj modify            IPEndPoint point1;
            IAsyncResult result1 = this._socket.BeginListen(controlSocket, null, null);
            try
            {
                this._ftp.WaitForCompletion(result1);
                return this._socket.EndListen(result1);
            }
            catch (ProxySocketException exception1)
            {
                this._socket.Close();
                throw new FtpException(exception1.Message, exception1, FtpExceptionStatus.ConnectFailure);
            }
            catch (Exception exception2)
            {
                this._socket.Close();
                throw exception2;
            }
//jyj modify            return point1;
        }

        public int Transfer()
        {
            if (!this._transfer)
            {
                return -1;
            }
            int num1 = this.DoTransfer();
            if (num1 == 0)
            {
                return 0;
            }
            if (num1 > 0)
            {
                this._length += ((long) num1);
                return num1;
            }
            try
            {
                this._socket.Shutdown(SocketShutdown.Send);
            }
            catch
            {
            }
            this._socket.Close();
            this._socket = null;
            this._transfer = false;
            return -1;
        }


        // Properties
        public long Length
        {
            get
            {
                return this._length;
            }
        }

        public FtpTransferState State
        {
            get
            {
                return this._state;
            }
        }


        // Fields
        protected byte[] _array;
        private bool _disposed;
        private Ftp _ftp;
        protected FtpStream _ftpStream;
        private long _length;
        protected ProxySocket _socket;
        private FtpTransferState _state;
        protected Stream _stream;
        private bool _transfer;
        protected const int DownloadBufferLength = 65536;
        protected const int UploadBufferLength = 4096;
    }}

